home *** CD-ROM | disk | FTP | other *** search
/ Group 42-Sells Out! - The Information Archive / Group 42 Sells Out (Group 42) (1996).iso / crypto / rc4week.txt < prev    next >
Internet Message Format  |  1995-11-18  |  14KB

  1. From patrick@Verity.COM Sun Oct 15 15:37:21 1995
  2. Date: Fri, 22 Sep 1995 09:27:25 -0700
  3. From: Patrick Horgan <patrick@Verity.COM>
  4. To: cypherpunks@toad.com
  5. Subject: Reformatted Weak Keys in RC4 for readability.
  6.  
  7. I modified only the formatting to get rid of the wrap...I did it so that I
  8. could enjoy reading it and I send it in to y'all so you won't have to do
  9. it as well...it's a wonderful paper.
  10.  
  11. Patrick
  12.                   A CLASS OF WEAK KEYS IN THE RC4 STREAM CIPHER
  13.  
  14.                                 PRELIMINARY DRAFT
  15.  
  16.                                    ANDREW ROOS
  17.                           VIRONIX SOFTWARE LABORATORIES
  18.  
  19.  
  20. 1.  INTRODUCTION
  21.  
  22. This paper discusses a class of weak keys in RSA's RC4 stream cipher. It shows
  23. that for at least 1 out of every 256 possible keys the initial byte of the
  24. pseudo-random stream generated by RC4 is strongly correlated with only a few
  25. bytes of the key, which effecitively reduces the work required to exhaustively
  26. search RC4 key spaces.
  27.  
  28.  
  29. 2.  STATE TABLE INITIALIZATION IN RC4
  30.  
  31. Although the RC4 algorithm has not been published by RSA Data Security, source
  32. code to implement the algorithm was anonymously posted to the Cypherpunks   
  33. mailing list several months ago. The success of the Cypherpunks' brute-force
  34. attack on SSL with a 40-bit key indicates that the source code published did
  35. accurately implement RC4.
  36.  
  37. RC4 uses a variable length key from 1 to 256 bytes to initialize a 256-byte
  38. state table which is used for the subsequent generation of pseudo-random bytes.
  39. The state table is first initialized to the sequence {0,1,2,...,255}.   
  40. Then:
  41.  
  42. 1    index1 = 0;
  43. 2    index2 = 0;
  44. 3
  45. 4    for(counter = 0; counter < 256; counter++)
  46. 5    {
  47. 6        index2 = (key_data_ptr[index1] + state[counter] + index2) % 256;   
  48. 7        swap_byte(&state[counter], &state[index2]);
  49. 8        index1 = (index1 + 1) % key_data_len;
  50. 9    }
  51.  
  52. Note that the only line which directly affects the state table is line 7, when
  53. two bytes in the table are exchanged. The first byte is indexed by "counter",
  54. which is incremented for each iteration of the loop. The second byte is
  55. indexed by "index2" which is a function of the key. Hence each element of the
  56. state table will be swapped at least once (although possibly with itself),
  57. when it is indexed by "counter". It may also be swapped zero, one or more   
  58. times when it is indexed by "index2". If we assume for the moment that
  59. "index2" is a uniformly distributed pseudo-random number, then the probability
  60. that a particular single element of the state table will be indexed by
  61. "index2" at some time during the initialization routine is:
  62.  
  63.      P = 1 - (255/256) ^ 255
  64.        = 0.631
  65.  
  66. (The exponent is 255 because we can disregard the case when "index2" and
  67. "counter" both index the same element, since this will not affect its value.)
  68.  
  69. Conversely, there is a 37% probability that a particular element will _not_ be
  70. indexed by "index2" during initialization, so its final value in the state
  71. table will only be affected by a single swap, when it is indexed by "counter".
  72. Since key bytes are used sequentially (starting again at the beginning when the
  73. key is exhausted), this implies:
  74.  
  75. A.   Given a key length of K bytes, and E < K, there is a 37% probability that
  76.      element E of the state table depends only on elements 0..E (inclusive) of
  77.      the key.
  78.  
  79. (This is approximate since "index2" is unlikely to be uniformly   
  80. distributed.)
  81.  
  82. In order to make use of this, we need to determine the most likely values for
  83. elements of the state table. Since each element is swapped at least once (when
  84. it is indexed by "counter"), it is necessary to take into account the likely
  85. effect of this swap. Swapping is a nasty non-linear process which is hard to
  86. analyze. However, when dealing with the first few elements of the state table,
  87. there is a high probability that the byte with which the element is swapped
  88. has not itself been involved in any previous exchanges, and therefore retains
  89. its initial value {0,1,2,...,255}. Similarly, when dealing with the first few
  90. elements of the state table, there is also a significant probability that none
  91. of the state elements added to index2 in line 6 of the algorithm has been   
  92. swapped either.
  93.  
  94. This means that the most likely value of an element in the state table can be
  95. estimated by assuming that state[x] == x in the algorithm above. In this case,
  96. the algorithm becomes:
  97.  
  98. 1    index1 = 0;
  99. 2    index2 = 0;
  100. 3
  101. 4    for(counter = 0; counter < 256; counter++)
  102. 5    {
  103. 6        index2 = (key_data_ptr[index1] + counter + index2) % 256;   
  104. 7        state[counter] = index2;
  105. 8        index1 = (index1 + 1) % key_data_len;
  106. 9    }
  107.  
  108. Which can be reduced to:
  109.  
  110. B.   The most likely value for element E of the state table is:
  111.  
  112.      S[E] = X(E) + E(E+1)/2
  113.      where X(E) is the sum of bytes 0..E (inclusive) of the key.
  114.  
  115. (when calculating the sum of key elements, the key is considered to "wrap   
  116. around" on itself).
  117.  
  118. Given this analysis, we can calculate the probability for each element of the
  119. state table that it's value is the "most likely value" of B above. The easiest
  120. way to do this is to evaluate the state tables produced from a number of
  121. pseudo-randomly generated RC4 keys. The following table shows the results for
  122. the first 47 elements from a trial of 100 000 eighty-bit RC4 keys:
  123.  
  124.           Probability (%)
  125.  
  126. 0-7       37.0  36.8  36.2  35.8  34.9  34.0  33.0  32.2
  127. 8-15      30.9  29.8  28.5  27.5  26.0  24.5  22.9  21.6
  128. 16-23     20.3  18.9  17.3  16.1  14.7  13.5  12.4  11.2
  129. 24-31     10.1   9.0   8.2   7.4   6.4   5.7   5.1   4.4
  130. 32-39      3.9   3.5   3.0   2.6   2.3   2.0   1.7   1.4
  131. 40-47      1.3   1.2   1.0   0.9   0.8   0.7   0.6   0.6
  132.  
  133. The table confirms that there is a significant correlation between the first
  134. few values in the state table and the "likely value" predicted by B.
  135.  
  136.  
  137. 3.  WEAK KEYS
  138.  
  139. The RC4 state table is used to generate a pseudo-random stream which is XORed
  140. with the plaintext to give the ciphertext. The algorithm used to generate the
  141. stream is as follows:
  142.  
  143.      x and y are initialized to 0.
  144.  
  145.      To generate each byte:
  146.  
  147. 1    x = (x + 1) % 256;
  148. 2    y = (state[x] + y) % 256;
  149. 3    swap_byte(&state[x], &state[y]);   
  150. 4    xorIndex = (state[x] + state[y]) % 256;   
  151. 5    GeneratedByte = state[xorIndex];
  152.  
  153. One way to exploit our analysis of the state table is to find circumstances
  154. under which one or more generated bytes are strongly correlated with a small
  155. subset of the key bytes.
  156.  
  157. Consider what happens when generating the first byte if state[1] == 1.
  158.  
  159. 1    x = (0 + 1) % 256;                  /* x == 1 */
  160. 2    y = (state[1] + 0) % 256;           /* y == 1 */
  161. 3    swap_byte(&state[1], &state[1]);    /* no effect */
  162. 4    xorIndex = (state[1] + state[1]);   /* xorIndex = 2 */
  163. 5    GeneratedByte = state[2]
  164.  
  165. And we know that state[2] is has a high probability of being
  166.  
  167.      S[2] = K[0] + K[1] + K[2] + 2 (2+1) / 2
  168.  
  169. Similarly,
  170.  
  171.      S[1] = K[0] + K[1] + 1 (1+1) / 2
  172.  
  173. So to make it probable that S[1] == 1, we have:
  174.  
  175.      K[0] + K[1] == 0 (mod 256)
  176.  
  177. In which case the most likely value for S[2] is:
  178.  
  179.      S[2] = K[2] + 3
  180.  
  181. This allows us to identify a class of weak keys:
  182.  
  183. C.   Given an RC4 key K[0]..K[N] with K[0] + K[1] == 0 (mod 256), there is a
  184.      significant probability that the first byte generated by RC4 will be   
  185.  
  186.      K[2] + 3 (mod 256).
  187.  
  188. Note that there are two special cases, caused by "unexpected" swapping during
  189. key generation. When K[0]==1, the "expected" output byte is k[2] + 2, and when
  190. k[0]==2, the expected value is k[2] + 1.
  191.  
  192. There are a number of similar classes of "weak keys" which only affect a few
  193. keys out of every 65536. However the particular symmetry in this class means
  194. that it affects one key in 256, making it the most interesting instance.
  195.  
  196. Once again I took the easy way out and used simulation to determine the
  197. approximate probability that result C holds for any given key. Probabilities
  198. ranged between 12% and 16% depending on the values of K[0] and K[1], with a
  199. mean of about 13.8%. All these figures are significantly greater than the   
  200. 0.39% which would be expected from an uncorrelated generator. The key length
  201. used was again 80 bits. This works the other way around as well: given the
  202. first byte B[0] generated by a weak key, the probability that K[2]==B[0]-3
  203. (mod 256) is 13.8%.
  204.  
  205.        
  206.  
  207. 4.  EXPLOITING WEAK KEYS IN RC4
  208.  
  209. Having found a class of weak keys, we need a practical way to attack RC4 based
  210. cryptosystems using them. The most obvious way would be to search potential
  211. weak keys first during an exhaustive attack. However since only one in every
  212. 256 keys is weak, the effective reduction in search space is not particularly
  213. significant.
  214.  
  215. The usefulness of weak keys does increase if the opponent is satisfied with
  216. recovering only a percentage of the keys subjected to analysis. Given a known
  217. generator output which includes the first generated byte, one could assume
  218. that the key was weak and search only the weak keys which would generate the
  219. known initial byte. Since 1 in 256 keys is weak, and there is a 13.8% chance
  220. that the assumed value of K[2] will be correct, there is only a 0.054% chance
  221. of finding the key this way. However, you have reduced the search space by 16
  222. bits due to the assumed relationship between K[0] and K[1] and the assumed
  223. value of K[2], so the work factor per key recovered is reduced by a factor of
  224. 35, which is equivalent reducing the effective key length by 5.1 bits.
  225.  
  226. However in particular circumstances, the known relationships between weak keys
  227. may provide a much more significant reduction in workload. The remainder of
  228. this section describes an attack which, although requiring very specific
  229. conditions, illustrates the potential threat.
  230.  
  231. As a stream cipher, a particular RC4 key can only be used once. When multiple
  232. communications sessions are required, some mechanism must be provided for   
  233. generating a new session key each time. Let us suppose that an implementation
  234. chose the simple method of incrementing the previous session key to get the
  235. new session key, and that the session key was treated as a "little endian"
  236. (least significant byte first) integer for this purpose.
  237.  
  238. We now have the interesting situation that the session keys will "cycle
  239. through" weak keys in a pattern which repeats every 2^16 keys:
  240.  
  241. 00 00 00 ...    Weak
  242. (510 non-weak keys)
  243. FF 01 00 ...    Weak
  244. (254 non-weak keys)
  245. FE 02 00 ...    Weak
  246. (254 non-weak keys)
  247. FD 03 00 ...    Weak
  248. ...
  249. 01 FF 00 ...    Weak
  250. (254 non-weak keys)
  251. 00 00 01 ...    Weak
  252. (510 non-weak keys)
  253. FF 01 01 ...    Weak
  254.  
  255. (Least significant byte on the left)
  256.  
  257. Now while an isolated weak key cannot be identified simply from a known
  258. generator output, this cycle of weak keys at known intervals can be identified
  259. using statistical techniques since each of the weak keys has a higher than
  260. expected probability of generating the _same_ initial byte. This means that an
  261. opponent who knew the initial generated bytes of about 2^16 session keys could
  262. identify the weak keys, and would also be able to locate the 510-key gap
  263. between successive cycles of weak keys (although not precisely). Since the
  264. 510-key gap occurs immediately following a key which begins with 00 00, the
  265. opponent not only knows that the keys are weak, but also knows the first two
  266. bytes of each key. The third byte of each key can be guessed from the first
  267. output byte generated by the key, with a 13.8% chance of a correct guess.   
  268.  
  269. Assuming that the "510-key gap" is narrowed down to 1 of 8 weak keys, the   
  270. attacker can search a key space which is 24 bits less than the size of the
  271. session keys, with a 13.8%/8 chance of success, effectively reducing the key
  272. space by approximately 18 bits.
  273.  
  274. Although this particular attack depends on a very specific set of
  275. circumstances, it is likely that other RC4 based cryptosystems in which there
  276. are linear relationships between successive session keys could be vulnerable
  277. to similar attacks.
  278.  
  279.  
  280. 5.  RECOMMENDATIONS
  281.  
  282. The attacks described in this algorithm result from inadequate "mixing" of key
  283. bytes during the generation of the RC4 state table. The following measures
  284. could be taken to strengthen cryptosystems based on the RC4 algorithm:
  285.  
  286. (a) After initializing the algorithm, generate and discard a number of   
  287. bytes.
  288.     Since the algorithm used to generate bytes also introduces additional   
  289.  
  290.     non-linear dependencies into the state table, this would make analysis
  291.     more difficult.
  292.  
  293. (b) In systems which require multiple session keys, ensure that session keys
  294.     are not linearly related to each other.
  295.  
  296. (c) Avoid using the weak keys described.
  297.  
  298.  
  299. 6.  CONCLUSION
  300.  
  301. This preliminary analysis of RC4 shows that the algorithm is vulnerable to
  302. analytic attacks based on statistical analysis of its state table. It is
  303. likely that a more detailed analysis of the algorithm will reveal more
  304. effective ways to exploit the weaknesses described.
  305.  
  306.  
  307.  
  308. Andrew Roos <andrewr@vironix.co.za>
  309.    _______________________________________________________________________
  310.   /  These opinions are mine, and not Verity's (except by coincidence;).  \
  311.  |                                                       (\                |
  312.  |  Patrick J. Horgan         Verity Inc.                 \\    Have       |
  313.  |  patrick@verity.com        1550 Plymouth Street         \\  _ Sword     | 
  314.  |  Phone : (415)960-7600     Mountain View                 \\/    Will    | 
  315.  |  FAX   : (415)960-7750     California 94303             _/\\     Travel | 
  316.   \___________________________________________________________\)__________/
  317.